package com.tg.appcommon.android;


import android.text.TextUtils;

import com.tange.base.toolkit.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class CustomInputStream {
    static public class InputStream {
        public byte[] mBa;
        private int    mPos;

        public InputStream(byte[] ba) {
            mBa = ba;
            mPos = 0;
        }
        public byte getByte() {
            return mBa[mPos++];
        }
        public int getUnsignedByte() {
            byte b = mBa[mPos++];
            if(b >= 0) return b;
            else {
                return (b & 0xFF);
            }
        }
        public int getUShort() {
            int result = CustomInputStream.getUShort(mBa, mPos);
            mPos += 2;
            return result;
        }
        public short getShort() {
            short result = CustomInputStream.getShort(mBa, mPos);
            mPos += 2;
            return result;
        }
        public int getInt() {
            int result = byteArrayToInt(mBa, mPos);
            mPos += 4;
            return result;
        }
        public String getString(int maxLen) {
            byte[] bytes = new byte[maxLen];
            System.arraycopy(mBa, mPos, bytes, 0, bytes.length);
            String encode= StringUtils.GuessEncoding(bytes);
            String str ="";
            if (!TextUtils.isEmpty(encode)) {

                try {
                    str = new String(bytes, 0, maxLen, encode).trim();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }
            TGLog.i("CustomInputStream","str ====== " + str+" encode: "+encode);
            int pos = str.indexOf("\0");
            if (pos != -1){
                str = str.substring(0, pos);
            }
            mPos += maxLen;
            return str;
        }
        public void skip(int n) {
            mPos += n;
        }


    }

    /* 转换short为byte */
    public static void putShort(byte b[], int index, short s) {
        b[index + 0] = (byte) (s >> 8);
        b[index + 1] = (byte) (s >> 0);
    }

    /** 通过byte数组取到short */
    public static short getShort(byte[] b, int index) {
        return (short) (((b[index + 0] << 8) | b[index + 1] & 0xff));
    }

    /** 将32位的int值放到4字节的byte数组 */
    public static byte[] intToByteArray(int num) {
        byte[] result = new byte[4];
        result[0] = (byte)(num >>> 24);//取最高8位放到0下标
        result[1] = (byte)(num >>> 16);//取次高8为放到1下标
        result[2] = (byte)(num >>> 8); //取次低8位放到2下标
        result[3] = (byte)(num );      //取最低8位放到3下标
        return result;
    }

    public static int getUShort(byte[] b, int index) {
        return (int) (  ( (((int)b[index + 1])&0xff) << 8)  |  (b[index + 0] & 0xff)  );
    }


    /** 将4字节的byte数组转成一个int值 */
    public static int byteArrayToInt(byte[] b, int index){
        byte[] a = new byte[4];
        int i = a.length - 1,j = index + 3;
        for (; i >= 0 ; i--,j--) {//从b的尾部(即int值的低位)开始copy数据
            if(j < b.length)
                a[i] = b[j];
            else
                a[i] = 0;//如果b.length不足4,则将高位补0
        }
        int v0 = (a[0] & 0xff) << 24;//&0xff将byte值无差异转成int,避免Java自动类型提升后,会保留高位的符号位
        int v1 = (a[1] & 0xff) << 16;
        int v2 = (a[2] & 0xff) << 8;
        int v3 = (a[3] & 0xff) ;
        return v0 + v1 + v2 + v3;
    }

    public static int byteArrayToInt(byte[] b) {
        return   b[3] & 0xFF |
                (b[2] & 0xFF) << 8 |
                (b[1] & 0xFF) << 16 |
                (b[0] & 0xFF) << 24;
    }
    /**
     * 字节数组转换成整数 关键技术：ByteArrayInputStream和DataInputStream
     *
     * @param byteArray
     * 需要转换的字节数组
     * @return
     */
    public static int bytesToInt(byte[] byteArray) {
        int n = 0;
        try {
            ByteArrayInputStream byteInput = new ByteArrayInputStream(byteArray);
            DataInputStream dataInput = new DataInputStream(byteInput);
            n = dataInput.readInt();
            byteInput.close();
            System.out.println("整数为： " + n);
        } catch (IOException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        return n;
    }
}
